﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;
using BUS;
using DATA.BusinessObjects;

namespace Presentation
{
    public partial class frmBaoCaoTheoMatDoSuDungPhong : Form
    {
        public frmBaoCaoTheoMatDoSuDungPhong()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLapBaoCao_Click(object sender, EventArgs e)
        {
            dgvChiTietBaoCao.Rows.Clear();

            double tongsongaythue = 0;
            ArrayList list_songaythue = new ArrayList();
            ArrayList list_tile = new ArrayList();

            IList<Phong> list_p = PhongBUS.LayDanhSachPhong();

            for (int i = 0; i < list_p.Count; i++)
            {
                int songaythue = 0;
                IList list_mix = BaoCaoBUS.LayBaoCaoMatDoSuDungPhongTheoMaPhong_Thang(list_p[i].Id, int.Parse(cbxThangCanLapBaoCao.SelectedItem.ToString()));
                for (int j = 0; j < list_mix.Count; j++)
                {
                    object[] temp = (object[])list_mix[j];
                    Phieuthuephong ptp = (Phieuthuephong) temp[0];
                    songaythue += Math.Abs(ptp.NgayThuePhong.Value.Subtract(ptp.NgayTraPhong.Value).Days);
                }
                tongsongaythue += songaythue;

                list_songaythue.Add(songaythue);
            }

            for (int i = 0; i < list_songaythue.Count; i++)
            {
                int tile = 0;
                if (tongsongaythue != 0)
                    tile = (int)((int)list_songaythue[i] * 100 / tongsongaythue);

                list_tile.Add(tile);
            }

            for (int i = 0; i < list_p.Count; i++)
            {
                String[] text = new String[] { (i + 1).ToString(), list_p[i].TenPhong, list_songaythue[i].ToString(), list_tile[i].ToString() + "%" };

                dgvChiTietBaoCao.Rows.Add(text);
            }
            txtTongSoNgayThue.Text = tongsongaythue.ToString();

            #region lưu xuống database
            int thang = int.Parse(cbxThangCanLapBaoCao.SelectedItem.ToString());
            if (MatDoSuDungPhongBUS.LayMatDoSuDungPhongTheoThang(thang) != null)
            {
                MessageBox.Show("Báo cáo thành công rồi !!!");
                return;
            }

            Matdosudungphong mdsdp = new Matdosudungphong();

            if (MatDoSuDungPhongBUS.LayMatDoSuDungPhongCuoiCung() != null)
                mdsdp.Id = ThamSoBUS.TaoMaTangTuDong(MatDoSuDungPhongBUS.LayMatDoSuDungPhongCuoiCung().Id, 6, "MMDSDP");
            else
                mdsdp.Id = "MMDSDP1";

            mdsdp.Thang = thang;
            //mdsdp.TongSoNgayThue = int.Parse(txtTongSoNgayThue.Text.ToString());

            MatDoSuDungPhongBUS.ThemMatDoSuDungPhong(mdsdp);

            for (int i = 0; i < list_p.Count; i++)
            {
                Chitietmatdosudungphong ctmdsdp = new Chitietmatdosudungphong();
                if (ChiTietMatDoSuDungPhongBUS.LayChiTietMatDoSuDungPhongMoiNhat() != null)
                    ctmdsdp.Id = ThamSoBUS.TaoMaTangTuDong(ChiTietMatDoSuDungPhongBUS.LayChiTietMatDoSuDungPhongMoiNhat().Id, 8, "MCTMDSDP");
                else
                    ctmdsdp.Id = "MCTMDSDP1";

                ctmdsdp.Matdosudungphong = mdsdp;
                ctmdsdp.SoNgayThue = int.Parse(list_songaythue[i].ToString());
                ctmdsdp.Phong = list_p[i];
                ctmdsdp.TyLe = int.Parse(list_tile[i].ToString());

                ChiTietMatDoSuDungPhongBUS.ThemChiTietMatDoSuDungPhong(ctmdsdp);
            }
            #endregion

            MessageBox.Show("Báo cáo thành công rồi !!!");
        }
    }
}
